package hkust.cse.calendar.unit;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

public class User implements Serializable {

	private String mPassword;				// User password
	private String mID;						// User id

	// Getter of the user id
	public String ID() {		
		return mID;
	}

	// Constructor of class 'User' which set up the user id and password
	public User(String id, String pass) {
		mID = id;
		mPassword = pass;

	}

	// Another getter of the user id
	public String toString() {
		return ID();
	}

	// Getter of the user password
	public String Password() {
		return mPassword;
	}

	// Setter of the user password
	public void Password(String pass) {
		mPassword = pass;
	}
	// Writing all the data from UserAccount.txt to the map
	public Map<String, String> getmap(){
		String UserAccount = "src/hkust/cse/calendar/unit/UserAccount.txt";	// cannot read the file
		Map<String,String> map = new HashMap<String, String>(); //the pair is username and password

		try {
			BufferedReader br = new BufferedReader(new FileReader(UserAccount ));

			String s;

			while ((s = br.readLine()) != null) {	// read the file line by line

				StringTokenizer st = new StringTokenizer(s);
				if(st.hasMoreTokens()){

					String username = st.nextToken();

					if(st.hasMoreTokens()){
						String password = st.nextToken();
						map.put(username, password);
					}


				}
			}

		} catch (FileNotFoundException e) {
			System.out.println("Can't find the file!");
		} catch (IOException e) {
			System.out.println("Reading error!");
		}
		return map;
	}
	// for login in
	public boolean login(String ID, String Password){
		Map<String, String> map = getmap();
		
		if(map.get(ID) == null){	// if the username cannot be found in the UserAccount List
			return false;
		}else{	// if the username can be found in the UserAccount List
			if(map.get(ID).equals(Password)){
				return true;
			}else{	// if the username and password do not match
				return false;	
			}
		}
	}
	// for checking whether the user already existed or not , for the purpose of creating account
	public boolean UsernameExisted(String ID){
		Map<String, String> map = getmap();
		
		if(map.get(ID) == null){	// if the username cannot be found in the UserAccount List
			return false;
		}else{	// if the username can be found in the UserAccount List
			return true;
		}
	}
	//for adding new account to the UserAccount.txt
	public void createNewAccount(String ID, String Password){
		String UserAccount = "src/hkust/cse/calendar/unit/UserAccount.txt";	// cannot read the file
		try {
			   File f = new File(UserAccount);
			   if(f.exists()){
			    System.out.print("File exists!\n");
			   }else{
			    System.out.print("File doesn't exist!");
			    f.createNewFile();
			   }
			   BufferedWriter output = new BufferedWriter(new FileWriter(f , true));
			   
			  
				
				output.write(ID + "\t" + Password +"\n"); 

			   
			   output.close();
			  } catch (Exception e) {
			   e.printStackTrace();
		}	  
	}




}
